<?php

class Backend_Controller_Picmaterials extends Backend_Controller_Abstract
{
	function actionGetpics()
	{
		$albumId = $this->_context->get('albumId', null);
//		$albumId = Util::infoDecryption($albumId);
		$photos = Pictures::model()->find('*', array('albumid' => $albumId), array('getFilePath' => true));
		if(empty($photos)){
			$photos = false;
		}
		$this->_returnAjax($photos);
	}
	
	function actionDeleteone()
	{
		$picId = $this->_context->post('picId', null);
		$this->_returnAjax(
			Pictures::model()->delete(array('id' => $picId))
		);
	}
	
	function actionRenameone()
	{
		$picId = $this->_context->post('picId', null);
		$newName = $this->_context->post('newName', null);
		$this->_returnAjax(
			Pictures::model()->update(array('name' => $newName), array('id' => $picId))
		);
	}
    
	function actionUploadlocal()
	{
//		$currAlbumId = $this->_context->get('curr_aid', 3);
//		$constraint = array(
//			'zTreeSetting' => array(
//				'isParent' => true,
//				'open' => true
//			)
//		);
//		$albumAllSimple = Albums::model()->find('*', null, $constraint);
//		$albumsTree = Albums::model()->getTree($albumAllSimple);											//fdump($albumsTree, 'albumsTree');
//		$albumsTreeWithEveryBranch = Albums::model()->getEveryBranchOfTree($albumsTree);	//fdump($albumsTreeWithEveryBranch, 'albumsTreeWithEveryBranch');
		
//		$this->_view['albumAllSimple'] = $albumAllSimple;
//		$this->_view['albumsTree'] = $albumsTree;
//		$this->_view['nodeTrunk'] = Albums::model()->getTrunkOfNode($albumsTreeWithEveryBranch, $currAlbumId);			//fdump($this->_view['nodeTrunk'], 'nodeTrunk');
//		$this->_view['nodeBranch'] = Albums::model()->getBranchOfNode($albumsTreeWithEveryBranch, $currAlbumId);		//fdump($this->_view['nodeBranch'], 'nodeBranch');
//		$this->_view['currNode'] = Albums::model()->find('*', array('id' => $currAlbumId), array('onlyOne' => true));	//fdump($this->_view['currNode'], 'currNode');
//		$this->_view['currAlbumId'] = $currAlbumId;
		
		$constraint = array(
			'zTreeSetting' => array(
				'isParent' => true,
				//'open' => true
			)
		);
		$this->_view['albumAllSimple'] = Albums::model()->find('*', null, $constraint);
		$this->_view['sessid'] = Util::infoEncryption(session_id());
	}
	
	function actionUploadgrabfromurl()
	{
		
	}
	
	function actionProcessuploadify()
	{
		$sessionId = Util::infoDecryption($_POST['sessid']);
		session_id($sessionId);
		session_start();
		if(empty($_POST['albumId']) || Albums::model()->count(array('id' => $_POST['albumId'])) <= 0){
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('NO-ALBUM-SELECTED')
			));
		}

		$quality = intval($this->_context->post('quality', PIC_DEFAULT_QUALITY));
		if($quality < 30 || $quality > 99){
			$quality = PIC_DEFAULT_QUALITY;
		}
		
		$now = time();
		$nowDate = date('Y-m-d H:i:s', $now);
		$albumId = $_POST['albumId'];
		
		$uploadResult = Pictures::model()->processUploadifyPhoto($albumId, $quality);
		if($uploadResult['result']){
			$pictureModel = new Pictures();
			$pictureModel->attributes = array(
				'albumid' => $albumId,
				'created_int' => $now,
				'created_date' => $nowDate,
				'size' => $uploadResult['orignalFileSize'],
				'quality' => $quality,
				'hasThumbnail' => 0,
				'type' => 'local'
			);
			if(is_readable($uploadResult['orignalFile'])){
				$pictureModel->attributes['filename'] = $uploadResult['fileName'];
				if(is_readable($uploadResult['thumbnailFile'])){
					$pictureModel->attributes['hasThumbnail'] = 1;
				}
				$pictureModel->save();
				
				$this->_returnAjax(array(
					'result' => true,
					'id' => $pictureModel->getConnection()->getLastInsert(),
					'fileName' => $uploadResult['fileName'],
					'orifileSrc' => $uploadResult['orignalFileSrc'],
					'thumbnailFileSrc' => $uploadResult['thumbnailFileSrc'],
					'size' => $uploadResult['orignalFileSize'],
					'quality' => $quality,
					'type' => 'local'
				));
			}
			
			$this->_returnAjax(array(
				'result' => false, 
				'error' => Util::getMessage('UNKNOWN-ERROR')
			));
		}else{
			$this->_returnAjax($uploadResult);
		}

	}
	
}